package com.aaa.check.service.impl;

import com.aaa.check.dao.DocheckAddDao;
import com.aaa.check.feign.UserFeign;
import com.aaa.check.service.DocheckAddService;
import com.aaa.entity.CheckResult;
import com.aaa.entity.User;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org. jdom2.CDATA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;


@Service
public class DecheckAddServiceImpl extends ServiceImpl<DocheckAddDao,CheckResult> implements DocheckAddService {

    @Resource
    private DocheckAddDao docheckAddDao;
    @Resource
    private UserFeign userFeign;

    @Override
    public boolean addd(CheckResult checkResult) {
        checkResult.setCreateTime(LocalDateTime.now());

        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenInfo = JWTUtils.getInfo(token);
        String phone = tokenInfo.get("username").toString();
        User byUsername = userFeign.getByUsername(phone);

        checkResult.setCreateBy(byUsername.getUserName());
        checkResult.setUpdateBy(byUsername.getUserName());
        int insert = docheckAddDao.insert(checkResult);
        return insert>0?true:false;
    }
}
